import java.io.ByteArrayInputStream;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;



public class XmlParser 
{	
    public  final int CONTENT_NODE = 1;
    public  final int LINK_NODE = 2;
    public  final int UPDATE_TAG = 3;
    public  final int LAST_MODIFIED = 4;

    public  void print_update_fields(Node node, String field, int p, HashMap<String, String> hm )
    {
        Element NewUpdateElement = (Element)node;
        NodeList ListUpdated = NewUpdateElement.getElementsByTagName(field);
        NodeList ListItems = ListUpdated.item(0).getChildNodes();
        hm.put("contentof_" + field + p, ((Node)ListItems.item(0)).getNodeValue().trim());
    }



    public  String print_tag(Node node, int op, int p, HashMap<String, String> hm)

    {

        switch(op)
        {
            case CONTENT_NODE:
                if(node.getNodeType() == 1)
                {
                    Element NewContentNode = (Element)node;
                    NodeList ListContents = NewContentNode.getElementsByTagName("content");
                    Element GetAttributeSrc = (Element)ListContents.item(0);
                    hm.put("src_content" + p, GetAttributeSrc.getAttribute("src"));
                }
                break;
            case LINK_NODE:
                if(node.getNodeType() == 1)
                {
                    Element NewLink = (Element)node;
                    NodeList ListLinks = NewLink.getElementsByTagName("link");
                    for(int j=0;j<NewLink.getElementsByTagName("link").getLength();j++)
                    {

                        Element GetEditMedia = (Element) ListLinks.item(j);			   	
                        if(GetEditMedia.getAttribute("rel").contentEquals("edit-media"))
                        {
                            GetEditMedia.getAttribute("rel").contentEquals("edit-media");
                            hm.put("href_editmedia" + p, GetEditMedia.getAttribute("href"));
                        }
                        Element GetEdit = (Element)ListLinks.item(j);
                        if(GetEdit.getAttribute("rel").contentEquals("edit"))
                        {	
                            GetEdit.getAttribute("rel").contentEquals("edit");
                            hm.put("href_edit" + p, GetEdit.getAttribute("href"));
                        }
                    }
                }
                break;
            case UPDATE_TAG:
                if(node.getNodeType() == 1)
                {
                    print_update_fields(node, "updated", p, hm);
                    print_update_fields(node, "title", p, hm);
                    print_update_fields(node, "gd:resourceId", p, hm);

                }

                break;
            case  LAST_MODIFIED:
                if(node.getNodeType() == 1)
                {
                    print_update_fields(node, "name", p, hm);
                    print_update_fields(node, "email", p, hm);

                }
                break;

        }
        return null;
    }



    public  void  googleDocParser(String argv,  HashMap<String, String> hm ) 
    {
        try 
        {


            DocumentBuilderFactory DocBuilFac = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = DocBuilFac.newDocumentBuilder();


            String XmlGoogleDoc = argv;
            Document doc= db.parse(new InputSource(new ByteArrayInputStream(XmlGoogleDoc.getBytes("utf-8"))));

            doc.getDocumentElement().normalize();          

            NodeList EntryList = doc.getElementsByTagName("entry");
            for(int i=0;i<EntryList.getLength();i++)
            {
                Element EtagAttri = (Element) EntryList.item( i );
                hm.put("gd:etag" + i , EtagAttri.getAttribute("gd:etag"));

                //print content
                print_tag(EntryList.item(i), CONTENT_NODE, i, hm);


                //print link
                print_tag(EntryList.item(i), LINK_NODE, i, hm);

                //print updated
                print_tag(EntryList.item(i), UPDATE_TAG,i,hm);

                NodeList EntryList1 = doc.getElementsByTagName("gd:lastModifiedBy");
                //print last modified
                print_tag(EntryList1.item(i),  LAST_MODIFIED, i, hm);

            }//the first for loop


        } //try block

        catch (Exception e)
        {
            e.printStackTrace();

        } //catch block

    } //main
}//finalXmlParser





